package de.tum.in.eist.poll.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.ValuePicker;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.NamedFrame;
import com.google.gwt.user.client.ui.SimpleRadioButton;
import com.google.gwt.user.client.ui.DecoratedTabBar;
import com.google.gwt.user.client.ui.TabBar;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.CaptionPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class EIST_POLL implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	public static class QuestionOneWidget extends Composite {
		private RadioButton rdbtnVeryUnsatisfied = new RadioButton("q1",
				"Very unsatisfied");
		private RadioButton rdbtnUnsatisfied = new RadioButton("q1",
				"Unsatisfied");
		private RadioButton rdbtnNeutral = new RadioButton("q1", "Neutral");
		private RadioButton rdbtnSatisfied = new RadioButton("q1", "Satisfied");
		private RadioButton rdbtnVerySatisfied = new RadioButton("q1",
				"Very Satisfied");
		private Label lblHowSatisfied = new Label(
				"How satisfied are you with today's lecture? \n");

		public QuestionOneWidget() {
			VerticalPanel panel = new VerticalPanel();
			panel.add(lblHowSatisfied);
			panel.add(rdbtnVeryUnsatisfied);
			panel.add(rdbtnUnsatisfied);
			panel.add(rdbtnNeutral);
			panel.add(rdbtnSatisfied);
			panel.add(rdbtnVerySatisfied);
			initWidget(panel);
		}
	}

	public static class QuestionWidget extends Composite {
		private Label lblWhatDid;
		private TextArea textArea = new TextArea();

		public QuestionWidget(String s) {
			VerticalPanel panel = new VerticalPanel();
			lblWhatDid = new Label(s);
			textArea.setSize("300px", "103px");
			panel.add(lblWhatDid);
			panel.add(textArea);
			initWidget(panel);
		}
	}

	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		RootPanel rootPanel = RootPanel.get();
		final QuestionOneWidget q1w = new QuestionOneWidget();
		final QuestionWidget q3w = new QuestionWidget(
				"What did you dislike or did not understand at all?");
		final QuestionWidget q2w = new QuestionWidget("What did you like most?");
		final TabPanel tp = new TabPanel();
		tp.setVisible(false);
		final Label lblIfYoureHappy = new Label(
				"If you're happy with you're answer, click the Button to submit it.");
		lblIfYoureHappy.setVisible(false);
		final Label lblAlreadySubmitted = new Label(
				"You have already submitted your feedback for this lecture.\nPlease come back later!");
		lblAlreadySubmitted.setVisible(false);

		tp.setSize("331px", "170px");
		tp.add(q1w, "  Question 1 ");
		q1w.setHeight("130px");
		tp.add(q2w, "  Question 2  ");
		q2w.setHeight("130px");
		tp.add(q3w, "  Question 3  ");
		tp.selectTab(0);

		rootPanel.add(tp, 337, 63);
		rootPanel.add(lblIfYoureHappy, 323, 42);
		rootPanel.add(lblAlreadySubmitted, 266, 139);

		final Button btnSubmit = new Button("Submit");
		btnSubmit.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (q1w.rdbtnVeryUnsatisfied.getValue()) {
					Window.alert("Submitted Very Unsatisfied!" + "\nFrage 2: "
							+ q2w.textArea.getText().toString() + "\nFrage 3: "
							+ q3w.textArea.getText().toString());
				} else if (q1w.rdbtnUnsatisfied.getValue()) {
					Window.alert("Submitted Unsatisfied!" + "\nFrage 2: "
							+ q2w.textArea.getText().toString() + "\nFrage 3: "
							+ q3w.textArea.getText().toString());
				} else if (q1w.rdbtnNeutral.getValue()) {
					Window.alert("Submitted Neutral!" + "\nFrage 2: "
							+ q2w.textArea.getText().toString() + "\nFrage 3: "
							+ q3w.textArea.getText().toString());
				} else if (q1w.rdbtnSatisfied.getValue()) {
					Window.alert("Submitted Satisfied!" + "\nFrage 2: "
							+ q2w.textArea.getText().toString() + "\nFrage 3: "
							+ q3w.textArea.getText().toString());
				} else if (q1w.rdbtnVerySatisfied.getValue()) {
					Window.alert("Submitted Very Satisfied!" + "\nFrage 2: "
							+ q2w.textArea.getText().toString() + "\nFrage 3: "
							+ q3w.textArea.getText().toString());
				} else {
					Window.alert("Nothing submitted, please choose again!\n(You have to answer Question 1!)");
					return;
				}
				tp.setVisible(false);
				lblIfYoureHappy.setVisible(false);
				btnSubmit.setVisible(false);
				Window.alert("Thank you for your Feedback!");
				lblAlreadySubmitted.setVisible(true);
			}
		});
		rootPanel.add(btnSubmit, 605, 254);
		btnSubmit.setVisible(false);

		final CaptionPanel cptnpnlLogin = new CaptionPanel("Login");
		final VerticalPanel verticalPanel = new VerticalPanel();
		final TextBox loginBox = new TextBox();
		final PasswordTextBox pswrdBox = new PasswordTextBox();
		pswrdBox.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getCharCode() == KeyCodes.KEY_ENTER) {
					loginBox.getText();
					pswrdBox.getText();
					cptnpnlLogin.setVisible(false);
					tp.setVisible(true);
					lblIfYoureHappy.setVisible(true);
					btnSubmit.setVisible(true);
					
				}
			}
		});

		verticalPanel.setSize("185px", "80px");
		verticalPanel.add(loginBox);
		verticalPanel.add(pswrdBox);
		cptnpnlLogin.setSize("185px", "85px");
		cptnpnlLogin.add(verticalPanel);
		rootPanel.add(cptnpnlLogin, 22, 15);

	}
}
